﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using C.Unite;
using C.Entities;
using ChanExt;
using MySoft.Data;
using System.Text;
using LitJson;

namespace ArtLand.OA.Project.HR
{
    public partial class HR_EmpList : CommonPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                btnNew.OnClientClick = Window1.GetShowReference("/HR/HR_EmpChange.aspx", "新增员工");// "TabView('', 'Add_Emp', '新增员工', '');";// Window1.GetShowReference("SysUserChange.aspx", "新增管理员");
                if (!string.IsNullOrEmpty(Request["dname"]))
                {
                    Grid1.Title += " - " + Request["dname"];
                }
                BindGrid(GetWhere());
            }
        }

        #region BindGrid
        private void BindGrid(WhereClip wc)
        {
            Grid1.SelectedRowIndex = -1;
            // 1.设置总项数
            Grid1.RecordCount = TbsDbSession.Default.From<V_EmpInfo>().Where(wc).Count();

            List<V_EmpInfo> items = TbsDbSession.Default.From<V_EmpInfo>().Where(wc).OrderBy(V_EmpInfo._.ID.Desc).Page(Grid1.PageSize, (Grid1.PageIndex + 1)).ToList();

            // 3.绑定到Grid
            Grid1.DataSource = items;
            Grid1.DataBind();
        }



        protected void Grid1_PageIndexChange(object sender, ChanExt.GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid(GetWhere());
        }


        #endregion

        #region Events

        public WhereClip GetWhere()
        {
            WhereClip wc = WhereClip.All;
            if (!String.IsNullOrEmpty(ttbxMyBox2.Text))
            {
                wc = wc && (V_EmpInfo._.EMNAME.Contain(ttbxMyBox2.Text) || V_EmpInfo._.EMPLID.Contain(ttbxMyBox2.Text));
            }
            if (!string.IsNullOrEmpty(Request["did"]))
            {
                OA_Department dep = TbsDbSession.Default.From<OA_Department>().Where(OA_Department._.DepID == int.Parse(Request["did"])).ToFirst();
                wc = wc && (V_EmpInfo._.DepPath.Contain(dep.DepPath + dep.DepID + "|"));
            }
            return wc;
        }



        // 点击 TwinTriggerBox 的搜索按钮
        protected void ttbxMyBox2_Trigger2Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(ttbxMyBox2.Text))
            {
                // 执行搜索动作
                BindGrid(GetWhere());
                ttbxMyBox2.ShowTrigger1 = true;
            }
            else
            {
                Alert.ShowInTop("请输入你要搜索的关键词！");
            }
        }

        // 点击 TwinTriggerBox 的取消按钮
        protected void ttbxMyBox2_Trigger1Click(object sender, EventArgs e)
        {
            // 执行清空动作

            ttbxMyBox2.Text = "";
            ttbxMyBox2.ShowTrigger1 = false;
            BindGrid(GetWhere());
        }


        #endregion
        protected void Grid1_RowDataBound(object sender, ChanExt.GridRowEventArgs e)
        {
            string name = e.Values[1].ToString();
            string stats = e.Values[6].ToString();
            string empid = e.Values[13].ToString();
            string conid = e.Values[12].ToString();
            switch (stats)
            {
                case "1":
                    e.Values[6] = "已转正";
                    break;
                case "2":
                    e.Values[6] = "试用期";
                    break;
                case "3":
                    e.Values[6] = "已离职";
                    break;
                case "4":
                    e.Values[6] = "已退休";
                    break;
                case "5":
                    e.Values[6] = "临时";
                    break;
                default:
                    e.Values[6] = "未设定合同";
                    break;
            }
            if (conid == "")
                e.Values[8] = "<a href=\"javascript:;\" onclick=\"" + Window2.GetShowReference("HR_ContractChange.aspx?empid=" + empid, "设定合同") + "\">设定合同</a>";
            else
                e.Values[8] = "<a href=\"javascript:;\" onclick=\"" + Window2.GetShowReference("HR_ContractChange.aspx?conid=" + conid, "查看合同") + "\">查看合同</a>";

            //if (conid == "")
            e.Values[9] = "<a href=\"javascript:;\" onclick=\"" + Window3.GetShowReference("HR_SalaryChange.aspx?empid=" + empid, "设定薪资 - " + name + "") + "\">设定薪资</a>";
            e.Values[10] = "<a href=\"javascript:;\" onclick=\"TabView('/HR/HR_InsuranceList.aspx?empID=" + empid + "', 'insur_" + empid + "', '" + name + "的保险缴纳情况', '')\">管理保险缴纳</a>";
            //else
            // e.Values[8] = "<a href=\"javascript:;\" onclick=\"" + Window2.GetShowReference("HR_ContractChange.aspx?conid=" + conid, "查看合同") + "\">查看合同</a>";
        }

        protected void Grid1_RowCommand(object sender, ChanExt.GridCommandEventArgs e)
        {
            int id = int.Parse(Grid1.DataKeys[e.RowIndex][0].ToString());
            if (e.CommandName == "Del")
            {
                TbsDbSession.Default.Delete<HR_EMP>(HR_EMP._.ID == id);
                Alert.ShowInTop("删除成功！");
                BindGrid(GetWhere());
            }

            //if (e.CommandName == "CheckBox1")
            //{
            //    CheckBoxField checkField = (CheckBoxField)Grid1.FindColumn(e.ColumnIndex);
            //    bool checkState = checkField.GetCheckedState(e.RowIndex);
            //    Sys_Users tea = new Sys_Users();
            //    tea.UserID = id;
            //    tea.Attach();
            //    tea.IsLock = checkState;
            //    TbsDbSession.Default.Update<Sys_Users>(tea);
            //    //TbsDbSession.Default.FromSql("update Sys_Users set IsLock='" + checkState + "' where id=" + id).ExecuteNonQuery();
            //    BindGrid(GetWhere());
            //}
        }


    }
}